import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:permission_handler/permission_handler.dart';

class PermissionTest extends StatefulWidget {
  @override
  _PermissionTestState createState() => _PermissionTestState();
}

/// PermissionStatus：
/// granted: 准予
/// denied: 拒绝
/// permanentlyDenied: 永久拒绝
/// limited: 有限
/// restricted: 限制

class _PermissionTestState extends State<PermissionTest> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('权限测试'),
      ),
      body: SingleChildScrollView(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Column(
              children: [
                ElevatedButton(
                  child: Text('缓存权限'),
                  onPressed: () async {
                    Map<Permission, PermissionStatus> status =
                        await [Permission.storage].request();
                    bool hasPermission =
                        status[Permission.storage] == PermissionStatus.granted;
                    Fluttertoast.showToast(
                        msg: hasPermission ? '有这个权限' : '没有这个权限');
                  },
                ),
                TextButton(
                  child: Text('摄像头权限'),
                  onPressed: () async {
                    Map<Permission, PermissionStatus> status =
                        await [Permission.camera].request();
                    bool hasPermission =
                        status[Permission.camera] == PermissionStatus.granted;
                    Fluttertoast.showToast(
                        msg: hasPermission ? '有这个权限' : '没有这个权限');
                  },
                ),
                ElevatedButton(
                  child: Text('申请多个权限'),
                  onPressed: () async {
                    Map<Permission, PermissionStatus> status = await [
                      Permission.photos,
                      Permission.microphone,
                    ].request();
                    if (status[Permission.photos] == PermissionStatus.granted &&
                        status[Permission.microphone] ==
                            PermissionStatus.granted) {
                      Fluttertoast.showToast(msg: '两个权限都获取到了');
                    } else {
                      Fluttertoast.showToast(msg: '获取失败');
                    }
                  },
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
